package com.ugoodtech.mdcc.web.service.impl;

import com.ugoodtech.mdcc.core.domain.KpiPaintReportVO.OperationalVO;
import com.ugoodtech.mdcc.core.domain.KpiPaintReportVO.ServiceResponseVo;
import com.ugoodtech.mdcc.core.domain.VstarCustomerSatisfactionReport.CustomerStatVo;
import com.ugoodtech.mdcc.core.domain.VstarServiceReport.VstarServiceReport;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport;
import com.ugoodtech.mdcc.core.dto.StationResponse;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.utils.CustomXWPFDocument;
import com.ugoodtech.mdcc.core.utils.ExportWordUtil;
import com.ugoodtech.mdcc.core.utils.WordUtil;
import com.ugoodtech.mdcc.web.service.ExportReportService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExportReportServiceImpl implements ExportReportService {

    private static final Logger logger = LoggerFactory.getLogger(ExportReportServiceImpl.class);


    @Override
    public String genKpiReport(OperationalVO vo, String templetPath) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("a1}", vo.getStationName());
        param.put("{a1}", vo.getStationName());
        param.put("{a2}", vo.getAddress());
        param.put("{a3}", vo.getCustomerUser());
        if (StringUtils.isNotEmpty(vo.getAuditUser())) {
            param.put("{a4}", vo.getAuditUser());
            param.put("01}", vo.getAuditUser());
        } else {
            param.put("{a4}", "");
            param.put("01}", "");
        }
        SimpleDateFormat in = new SimpleDateFormat("dd/MM/yyyy");
        param.put("{a5}", in.format(new Date()));
        param.put("02}", vo.getCompany());

        String imageUrl = "";
        if (StringUtils.isNotEmpty(vo.getPhoto())) {
            imageUrl = "http://vstar.oss-cn-hangzhou.aliyuncs.com/" + vo.getPhoto();
        } else {
            imageUrl = "http://vstar.oss-cn-hangzhou.aliyuncs.com/backend/photo/1534053126167_%E5%85%89%E4%BA%AE%E5%BA%A6.png";
        }

        URL url = new URL(imageUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();//打开链接方式
        con.setRequestMethod("GET");//以GET方式请求连接
        con.setConnectTimeout(5 * 1000);//设置连接时间为5秒
        InputStream is = con.getInputStream();//输入流获取图片
        byte[] byt = readInputStream(is);

        Map<String, Object> header1 = new HashMap<String, Object>();
        header1.put("width", 450);
        header1.put("height", 300);
        header1.put("type", "png");
        header1.put("content", byt);
        param.put("{03}", header1);

//        Map<String,Object> header = new HashMap<String, Object>();
//        header.put("width", 100);
//        header.put("height", 150);
//        header.put("type", "jpg");
//        header.put("content", WordUtil.inputStream2ByteArray(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test.png"), true));
//        param.put("${header}",header);

//        List<String> svgList = vo.getResponseKpi();
//        if(null != svgList && svgList.size() > 0) {
//            ByteArrayOutputStream baossvg = new ByteArrayOutputStream();
//            WordUtil.convertToPng(svgList.get(0), baossvg);
//            Map<String,Object> svg1 = new HashMap<String, Object>();
//            svg1.put("width", 900);
//            svg1.put("height", 300);
//            svg1.put("type", "png");
//            svg1.put("content", baossvg.toByteArray());
//            param.put("Svg1",svg1);
//
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(1), baossvg);
////            Map<String,Object> svg2 = new HashMap<String, Object>();
////            svg2.put("width", 900);
////            svg2.put("height", 300);
////            svg2.put("type", "png");
////            svg2.put("content", baossvg.toByteArray());
////            param.put("Svg3",svg2);
////
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(2), baossvg);
////            Map<String,Object> svg3 = new HashMap<String, Object>();
////            svg3.put("width", 900);
////            svg3.put("height", 300);
////            svg3.put("type", "png");
////            svg3.put("content", baossvg.toByteArray());
////            param.put("Svg3",svg3);
////
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(3), baossvg);
////            Map<String,Object> svg4 = new HashMap<String, Object>();
////            svg4.put("width", 900);
////            svg4.put("height", 300);
////            svg4.put("type", "png");
////            svg4.put("content", baossvg.toByteArray());
////            param.put("Svg4",svg4);
//
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(4), baossvg);
////            Map<String,Object> svg5 = new HashMap<String, Object>();
////            svg5.put("width", 900);
////            svg5.put("height", 300);
////            svg5.put("type", "png");
////            svg5.put("content", baossvg.toByteArray());
////            param.put("Svg5",svg5);
//
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(5), baossvg);
////            Map<String,Object> svg6 = new HashMap<String, Object>();
////            svg6.put("width", 900);
////            svg6.put("height", 300);
////            svg6.put("type", "png");
////            svg6.put("content", baossvg.toByteArray());
////            param.put("Svg6",svg6);
////
////            baossvg = new ByteArrayOutputStream();
////            WordUtil.convertToPng(svgList.get(6), baossvg);
////            Map<String,Object> svg7 = new HashMap<String, Object>();
////            svg7.put("width", 900);
////            svg7.put("height", 300);
////            svg7.put("type", "png");
////            svg7.put("content", baossvg.toByteArray());
////            param.put("Svg7",svg7);
//
//            baossvg.close();
//        }

//        ByteArrayOutputStream baossvg = new ByteArrayOutputStream();
//        WordUtil.convertToPng("<svg version='1.1' class='highcharts-root' style='font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Arial, Helvetica, sans-serif;font-size:12px;' xmlns='http://www.w3.org/2000/svg' width='634' height='400' viewBox='0 0 634 400'><desc>Created with Highcharts 6.1.1</desc><defs><clipPath id='highcharts-1pwty3v-21'><rect x='0' y='0' width='550' height='353' fill='none'></rect></clipPath></defs><rect fill='#ffffff' class='highcharts-background' x='0' y='0' width='634' height='400' rx='0' ry='0'></rect><rect fill='none' class='highcharts-plot-background' x='74' y='10' width='550' height='353'></rect><g class='highcharts-grid highcharts-xaxis-grid ' data-z-index='1'><path fill='none' data-z-index='1' class='highcharts-grid-line' d='M 348.5 10 L 348.5 363' opacity='1'></path><path fill='none' data-z-index='1' class='highcharts-grid-line' d='M 623.5 10 L 623.5 363' opacity='1'></path><path fill='none' data-z-index='1' class='highcharts-grid-line' d='M 73.5 10 L 73.5 363' opacity='1'></path></g><g class='highcharts-grid highcharts-yaxis-grid ' data-z-index='1'><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 363.5 L 624 363.5' opacity='1'></path><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 292.5 L 624 292.5' opacity='1'></path><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 222.5 L 624 222.5' opacity='1'></path><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 151.5 L 624 151.5' opacity='1'></path><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 81.5 L 624 81.5' opacity='1'></path><path fill='none' stroke='#e6e6e6' stroke-width='1' data-z-index='1' class='highcharts-grid-line' d='M 74 9.5 L 624 9.5' opacity='1'></path></g><rect fill='none' class='highcharts-plot-border' data-z-index='1' x='74' y='10' width='550' height='353'></rect><g class='highcharts-axis highcharts-xaxis ' data-z-index='2'><path fill='none' class='highcharts-tick' stroke='#ccd6eb' stroke-width='1' d='M 348.5 363 L 348.5 373' opacity='1'></path><path fill='none' class='highcharts-tick' stroke='#ccd6eb' stroke-width='1' d='M 624.5 363 L 624.5 373' opacity='1'></path><path fill='none' class='highcharts-tick' stroke='#ccd6eb' stroke-width='1' d='M 73.5 363 L 73.5 373' opacity='1'></path><path fill='none' class='highcharts-axis-line' stroke='#ccd6eb' stroke-width='1' data-z-index='7' d='M 74 363.5 L 624 363.5'></path></g><g class='highcharts-axis highcharts-yaxis ' data-z-index='2'><text x='26.09375' data-z-index='7' text-anchor='middle' transform='translate(0,0) rotate(270 26.09375 186.5)' class='highcharts-axis-title' style='color:#666666;fill:#666666;' y='186.5'><tspan>维修产值</tspan></text><path fill='none' class='highcharts-axis-line' data-z-index='7' d='M 74 10 L 74 363'></path></g><g class='highcharts-series-group' data-z-index='3'><g data-z-index='0.1' class='highcharts-series highcharts-series-0 highcharts-column-series highcharts-color-0 highcharts-tracker' transform='translate(74,10) scale(1 1)' clip-path='url(#highcharts-1pwty3v-21)'><rect x='122.5' y='28.5' width='30' height='325' fill='#7cb5ec' stroke='#ffffff' stroke-width='1' class='highcharts-point highcharts-color-0'></rect><rect x='397.5' y='28.5' width='30' height='325' fill='#7cb5ec' stroke='#ffffff' stroke-width='1' class='highcharts-point highcharts-color-0'></rect></g><g data-z-index='0.1' class='highcharts-markers highcharts-series-0 highcharts-column-series highcharts-color-0 ' transform='translate(74,10) scale(1 1)' clip-path='none'></g></g><text x='317' text-anchor='middle' class='highcharts-title' data-z-index='4' style='color:#333333;font-size:18px;fill:#333333;' y='24'></text><text x='317' text-anchor='middle' class='highcharts-subtitle' data-z-index='4' style='color:#666666;fill:#666666;' y='24'></text><g class='highcharts-axis-labels highcharts-xaxis-labels ' data-z-index='7'><text x='211.5' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='middle' transform='translate(0,0)' y='382' opacity='1'>改善前1个月</text><text x='486.5' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='middle' transform='translate(0,0)' y='382' opacity='1'>改善当月</text></g><g class='highcharts-axis-labels highcharts-yaxis-labels ' data-z-index='7'><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='367' opacity='1'>0</text><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='296' opacity='1'>50</text><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='226' opacity='1'>100</text><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='155' opacity='1'>150</text><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='85' opacity='1'>200</text><text x='59' style='color:#666666;cursor:default;font-size:11px;fill:#666666;' text-anchor='end' transform='translate(0,0)' y='14' opacity='1'>250</text></g></svg>", baossvg);
//        Map<String,Object> header = new HashMap<String, Object>();
//        header.put("width", 900);
//        header.put("height", 300);
//        header.put("type", "png");
//        header.put("content", baossvg.toByteArray());
//        param.put("Svg1",header);
//        baossvg.close();


//        Map<String,Object> twocode = new HashMap<String, Object>();
//        twocode.put("width", 100);
//        twocode.put("height", 100);
//        twocode.put("type", "png");
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        ZxingHandler.encode("测试二维码,huangqiqing", 100,100, baos);
//        twocode.put("content", baos.toByteArray());
//        param.put("${twocode}",twocode);


//

        SimpleDateFormat inFile = new SimpleDateFormat("yyyyMMdd");
//        String filePath = templetPath + "\\export\\kpi" + inFile.format(new Date()) + ".docx";精益钣喷评估方案-维修站点名

        String fileName = "/export/精益钣喷评估方案-" + vo.getStationName() + inFile.format(new Date()) + ".docx";
        String filePath = templetPath + fileName;

        CustomXWPFDocument doc = WordUtil.generateWord(param, templetPath + "/templet/LeanPaintReport.docx");
        FileOutputStream fopts = new FileOutputStream(filePath);
        doc.write(fopts);
        fopts.close();

        ExportWordUtil.exportKpiToWord(vo, filePath, templetPath);
        return fileName;
    }


    @Override
    public String genWeekKpiReport(VstarUserKpiReport vstarUserKpiReport, String templetPath) throws Exception {

        Map<String, Object> header1 = new HashMap<String, Object>();

        SimpleDateFormat inFile = new SimpleDateFormat("yyyyMMdd");
        String fileName = "/export/常规报告列表" + inFile.format(new Date()) + ".docx";
        String filePath = templetPath + fileName;
        CustomXWPFDocument doc = WordUtil.generateWord(null, templetPath + "/templet/WeekReport.docx");
        FileOutputStream fopts = new FileOutputStream(filePath);
        doc.write(fopts);
        fopts.close();
        ExportWordUtil.exportWeekKpiToWord(vstarUserKpiReport, filePath, templetPath);
        return fileName;
    }

    @Override
    public String genCustomerStatReport(CustomerStatVo customerStatVo, String templetPath) throws Exception {
        SimpleDateFormat inFile = new SimpleDateFormat("yyyyMMdd");
//        String filePath = templetPath + "/export/VStarServiceReport" + inFile.format(new Date()) + ".docx";
        String fileName = "/export/满意度调查统计报告" + inFile.format(new Date()) + ".docx";
        String filePath = templetPath + fileName;
        CustomXWPFDocument doc = WordUtil.generateWord(null, templetPath + "/templet/VStarCustomerReport.docx");
        FileOutputStream fopts = new FileOutputStream(filePath);
        doc.write(fopts);
        fopts.close();
        ExportWordUtil.exportCustomerToWord(customerStatVo, filePath);
        return fileName;
    }

    @Override
    public String getServiceReport(ServiceResponseVo serviceResponseVo, String templetPath) throws Exception {
        SimpleDateFormat inFile = new SimpleDateFormat("yyyyMMdd");
        String fileName = "/export/漆星服务运营分析" + inFile.format(new Date()) + ".docx";
        String filePath = templetPath + fileName;
        CustomXWPFDocument doc = WordUtil.generateWord(null, templetPath + "/templet/VStarServiceReport.docx");
        FileOutputStream fopts = new FileOutputStream(filePath);
        doc.write(fopts);
        fopts.close();
        ExportWordUtil.exportServiceToWord(serviceResponseVo, filePath);
        return fileName;
    }

    @Override
    public String getStationInfo(List<StationResponse> stationResponses, String templetPath) throws Exception {
        SimpleDateFormat inFile = new SimpleDateFormat("yyyyMMdd");
        String fileName = "/export/维修站信息" + inFile.format(new Date()) + ".docx";
        String filePath = templetPath + fileName;
        CustomXWPFDocument doc = WordUtil.generateWord(null, templetPath + "/templet/StationInfo.docx");
        FileOutputStream fopts = new FileOutputStream(filePath);
        doc.write(fopts);
        fopts.close();
        ExportWordUtil.exportStationToWord(stationResponses, filePath);
        return fileName;
    }


    public static byte[] readInputStream(InputStream is) throws Exception {
        ByteArrayOutputStream outsream = new ByteArrayOutputStream();//创建一个字节数组输出流
        byte[] b = new byte[10240000];//创建byte数组并设置大小
        int len = 0;//设置图片起点存储位置
        while ((len = is.read(b)) != -1) {//输入流读取图片未读取结束
            outsream.write(b, 0, len);//边读写入b字节数组中
        }
        outsream.close();
        return outsream.toByteArray();//返回字节数组
    }
}